package coupon

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// ListItemConfigurationPtrsByCouponID 获取优惠券到商品配置数组
func (s *CouponEntityServiceImp) ListItemConfigurationPtrsByCouponID(ctx context.Context, q *sqlc.Queries, couponID int64) (arr param.ItemConfigurationPtrArray, err error) {
	couponItems, err := q.ListNormalCouponItemsByCouponID(ctx, couponID)
	if err != nil {
		return
	}
	for _, couponItem := range couponItems {
		arr = append(arr, &param.ItemConfiguration{
			ID:   couponItem.ItemID,
			Name: couponItem.ItemName,
		})
	}
	return
}

// GetMapOfCouponIDToItemConfigurationPtrsByCouponIDs 获取优惠券到商品配置的映射
func (s *CouponEntityServiceImp) GetMapOfCouponIDToItemConfigurationPtrsByCouponIDs(ctx context.Context, q *sqlc.Queries, couponIDs []int64) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	m = make(param.EntityIDToItemConfigurationPtrsMap)
	if len(couponIDs) == 0 {
		return
	}
	couponItems, err := q.ListNormalCouponItemsByCouponIDs(ctx, couponIDs)
	if err != nil {
		return
	}
	for _, couponItem := range couponItems {
		couponID := couponItem.CouponID
		valPtr := &param.ItemConfiguration{
			ID:   couponItem.ItemID,
			Name: couponItem.ItemName,
		}
		if val, ok := m[couponID]; ok {
			val = append(val, valPtr)
			m[couponID] = val
		} else {
			m[couponID] = []*param.ItemConfiguration{valPtr}
		}
	}
	return
}

// GetMapOfCouponIDToItemConfigurationPtrsByCouponPtrs 获取优惠券到商品配置的映射
func (s *CouponEntityServiceImp) GetMapOfCouponIDToItemConfigurationPtrsByCouponPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.Coupon) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	return s.GetMapOfCouponIDToItemConfigurationPtrsByCouponIDs(ctx, q, s.ConvToCouponIDs(ptrs))
}

// GetMapOfCouponIDToItemConfigurationPtrsByListCouponWithStatRowPtrs 获取优惠券到商品配置的映射
func (s *CouponEntityServiceImp) GetMapOfCouponIDToItemConfigurationPtrsByListCouponWithStatRowPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.ListCouponWithStatsRow) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	return s.GetMapOfCouponIDToItemConfigurationPtrsByCouponIDs(ctx, q, s.ConvToCouponIDs(ptrs))
}


// GetMapOfCouponIDToItemIDsByCouponIDs 获取优惠券到商品ID的映射
func (s *CouponEntityServiceImp) GetMapOfCouponIDToItemIDsByCouponIDs(ctx context.Context, q *sqlc.Queries, couponIDs []int64) (m param.EntityIDToItemIDMap, err error) {
	m = make(param.EntityIDToItemIDMap)
	if len(couponIDs) == 0 {
		return
	}
	couponItems, err := q.ListNormalCouponItemsByCouponIDs(ctx, couponIDs)
	if err != nil {
		return
	}
	for _, couponItem := range couponItems {
		couponID := couponItem.CouponID
		val := param.ItemID(couponItem.ItemID)
		if vals, ok := m[couponID]; ok {
			vals = append(vals, val)
			m[couponID] = vals
		} else {
			m[couponID] = []param.ItemID{val}
		}
	}
	return
}
